Base.js ➔ ???   A
last analyzed

Complexity

Conditions 1
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
c 6
b 0
f 0
nc 2
dl 0
loc 11
rs 9.4285
cc 1
nop 2
1
import { PureComponent } from 'react';
2
import { DEFAULT_SLICE } from '../constants';
3
4
class BaseRouterComponent extends PureComponent {
5
6
    constructor(props, context) {
7
        super(props, context);
8
9
        const { store, router } = context;
10
        this.store = store;
11
        this.router = router;
12
13
        this.handleStoreChange = this.handleStoreChange.bind(this);
14
15
        this.unsubscribe = store && store.subscribe(this.handleStoreChange);
16
    }
17
18
    componentDidMount() {
19
        return this.handleStoreChange();
20
    }
21
22
    componentWillUnmount() {
23
24
        if (this.isSubscribed) {
25
            this.unsubscribe();
26
            delete this.unsubscribe;
27
        }
28
    }
29
30
    get isSubscribed() {
31
32
        return typeof this.unsubscribe === 'function';
33
    }
34
35
    handleStoreChange() { // eslint-disable-line class-methods-use-this
36
        throw new Error('this.handleStoreChange() should be implemented');
37
    }
38
39
    getStatefromStore() {
40
        const { slice = DEFAULT_SLICE, immutable } = this.router;
41
        const state = this.store.getState();
42
        return immutable ? state.get(slice) : state[slice];
43
    }
44
}
45
46
export default BaseRouterComponent;
47